<?php

namespace App\Http\Controllers\Backstage\Article;

use App\Models\ArticleCate;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\URL;
use Illuminate\Http\Request;
//文章分类管理
class ArticleCateController extends Controller
{
    //获取文章分类
    public function getArticleCate(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $info = DB::table('article_cate')
            ->leftjoin('upload_file', 'article_cate.fileid', '=', 'upload_file.fileid')
            ->orderBy('catid', 'desc')
            ->get();
        if ($info) {
            $info = json_decode($info, true);
            foreach ($info as $key => $value) {
                $info[$key]['article_cate_time'] = date('Y-m-d', $value['article_cate_time']);
                $info[$key]['url'] =  http_type() . $_SERVER['HTTP_HOST'] . "/" . $info[$key]['url'] . "/" . $info[$key]['file_name'];
                foreach ($info as $k => $v) {
                    if ($value['catid'] == $v['parentid']) {
                        $info[$key]['children'][$k] = $v;
                        $info[$key]['children'] = array_values($info[$key]['children']);
                    }
                }
            }
            foreach ($info as $keys => $values) {
                if ($values['parentid'] != '0') {
                    unset($info[$keys]);
                }
            }
            $info = array_values($info);

            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }

    //获取全部分类
    public function getAllArticleCate(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $info = DB::table('article_cate')
            ->orderBy('catid', 'desc')->get();
        if ($info) {
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }

    //获取文章父级分类
    public function getParentArticleCate(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['parentid'] = 0;
        $info = DB::table('article_cate')
            ->where($map)
            ->orderBy('catid', 'desc')->get();
        if ($info) {
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }
    //新增文章分类
    public function addarticlecate(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $date['parentid'] = (int) $request->input('parentid');
        $date['fileid'] = (int) $request->input('fileid');
        $date['article_cate_title'] = addslashes($request->input('article_cate_title'));
        $date['article_cate_introduction'] = addslashes($request->input('article_cate_introduction'));
        $date['article_cate_time'] = time();
        $info = DB::table('article_cate')->insert($date);
        if ($info) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }
    //修改文章分类
    public function editarticlecate(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['catid'] = $request->input('catid');
        $date['fileid'] = (int) $request->input('fileid');
        $date['article_cate_title'] = $request->input('article_cate_title');
        $date['article_cate_introduction'] = addslashes($request->input('article_cate_introduction'));
        $info = DB::table('article_cate')->where($map)->update($date);
        if ($info) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }
    //删除文章分类
    public function delarticlecate(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['catid'] = $request->input('catid');
        $info = DB::table('article_cate')->where($map)->delete();
        if ($info) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }
    //获取文章分类状态
    public function getArticleStatus(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        // 文章分类样式1一级大图2一级小图3二级分类
        $map['status_id'] = 1;
        $info = DB::table('status')->where($map)->first();
        if ($info) {
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }
    //设置文章分类状态
    public function setArticleStatus(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        // 文章分类样式1一级大图2一级小图3二级分类
        $map['status_id'] = 1;
        $date['status'] = (int) $request->input('status');
        $msg = DB::table('status')->where($map)->update($date);
        if ($msg) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }
}
